定义 Python 字典:键值映射
在 Python 中,字典是一种灵活且基础的数据类型,用于以描述性而非位置性的方式存储数据。与列表或元组等序列不同,字典是无序的集合,其中数据通过唯一名称组织,这些名称被称为键键,而不是数值索引。这种结构实现了极快的数据查找速度。
1. 核心结构:键值对
数据以成对的形式存储,其中唯一的键直接映射到一个值。这种设计模拟了优化的哈希表,确保极快的检索速度。键必须是唯一的且不可变的(通常是字符串或数字),而值可以是任何数据类型。
{"student_id":101,"name":"Alex","grade":88.5}
2. Dictionary Syntax and Properties
- Dictionaries are defined using curly braces
{}. - The colon
:separates the Key from its corresponding Value (Key:Value). - Dictionaries are mutable, meaning keys and values can be dynamically added, deleted, or modified after creation.
💡 Indexing vs. Key Lookup
Dictionaries do not support numerical indexing (like
[0] or [1]). Data must be retrieved using the specific key name provided during creation (e.g., dictionary["name"]).
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>